﻿using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Transactions;

namespace RM.Data
{
    public class UnitOfWork:IUnitOfWork,IDisposable
    {
        private bool _disposed;
        private TransactionScope _transaction;
        private readonly RmDataModelContainer _db;

        public RmDataModelContainer Db
        {
            get { return _db; }
        }

        public UnitOfWork()
        {
            _db = new RmDataModelContainer();
        }

        public void StartTransaction()
        {
            _transaction = new TransactionScope();
        }

        public void Commit()
        {
            _db.SaveChanges();
            _transaction.Complete();
        }

        public void Dispose()
        {
            Dispose(true);
            GC.SuppressFinalize(this);
        }

        protected void Dispose(bool disposing)
        {
            if (!_disposed)
            {
                if (disposing)
                {
                    if (_transaction != null)
                    {
                        _transaction.Dispose();
                    }
                }
                _transaction = null;
                _disposed = true;
            }
        }
    }
}
